<?php echo form_open('kupon/cartupdate'); ?>
<p><input type="button" name="coupon" id="opener" value="Use Coupon"  /></p><br />
<p><div id="dialog">
	<input type="text" name="coupon_code" id="coupon_code" title="Your coupon code" />
    
</div></p>
<br />
<div id="result"></div>
<table  class="standard-table">

<tr>
  <th>QTY</th>
  <th>Item Description</th>
  <th style="text-align:right">Item Price</th>
  <th style="text-align:right">Sub-Total</th>
  <th></th>
</tr>

<?php $i = 1;
$sub_price = 0;
$total = 0; ?>

<?php foreach ($checkout->result_array() as $items): ?>

	<?php echo form_hidden('rowid[]', $items['id']); ?>

	<tr>
	  <td><?php echo form_input(array('name' => 'quantity[]', 'value' => $items['quantity'], 'maxlength' => '3', 'size' => '5', 'class' => 'spinner', 'title' => 'Quantity'));?></td>
	  <td>
		<?php echo $items['product_name']; 
		echo form_hidden('product_id[]', $items['product_id']);?>
	  </td>
	  <td><?php echo form_hidden('price[]', $items['price']); ?><?php echo number_format($items['price'],2); ?></td>
	  <td>RM<?php $sub_price = $items['price'] * $items['quantity']; echo number_format($sub_price,2); ?></td>
	  <td><a href="<?php echo base_url('kupon/cartdelete/'). '/'.$items['id']; ?>" title="Delete item">x</a></td>
	</tr>
<?php $total = $total + ($items['price'] * $items['quantity']);?>
<?php $i++; ?>

<?php endforeach; ?>

<tr>
  <td colspan="2"> </td>
  <td class="right"><strong>Total</strong></td>
  <td class="right"><span  id="tot_price"><?php echo 'RM '.number_format($total,2); ?></span>
  <?php echo form_hidden('total_price', $total); ?>
   <input type="hidden" name="diskaun_price" value="<?php echo $total;?>"/></td>
</tr>

</table>

<p><?php echo form_submit('update', 'Update your Cart');
	echo ' '.form_submit('pay', 'Proceed Payment'); ?></p>

<?php echo form_close()?>

<script>
$(function() {
	/*$( "#dialog" ).dialog({
		autoOpen: false,
		show: {
			effect: "blind",
			duration: 1000
		},
		hide: {
			effect: "explode",
			duration: 1000
		}
	});*/
	$("#dialog").hide();
	$( "#opener" ).click(function() {
		$("#dialog").toggle()
	});
	
	var cct = '<?php echo $this->security->get_csrf_hash(); ?>';
	$("#coupon_code").keyup(function(){
		//alert($("input[name=total_price]").val());
		$.post('<?php echo site_url();?>kupon/ajaxkupon',
			{	
				'<?php echo $this->config->item('csrf_token_name'); ?>': cct,
				'coupon_code' : $("#coupon_code").val(),
				'total_price' : $("input[name=total_price]").val()
			},
			function(data){
				var response = jQuery.parseJSON(data);
				$("#result").html(response.message);
				//$("#total_price").html(response.total_price);
				var totalprice = response.total_price;
				totalprice  = 'RM' + totalprice.toFixed(2);
				if(response.diskaun != 'NULL'){
					totalprice = totalprice + ' (discount ' + response.diskaun +')';
				}
				$("#tot_price").html(totalprice);
				$("input[name=diskaun_price]").val(response.total_price);
			}
		);
	});
	
	//$('input[title!=""]').hint();
	$(document).tooltip({track:true});
	//$( ".spinner" ).spinner({min : 1});
});
</script>